//
//  TransforController.m
//  WisdomZhuHai
//
//  Created by JasonLee on 15/1/16.
//  Copyright (c) 2015年 wyd. All rights reserved.
//

#import "TransforController.h"
#import "MobClick.h"

@interface TransforController()

@end

static BOOL isTestModel;
static TransforController *shanrdObject = nil;
static ThirdViewController *static_ThirdViewController = nil;
static SecondViewController *static_SecondViewController = nil;
static PersonViewController *static_PersonViewController = nil;
static ToolsViewController *static_ToolsViewController = nil;
static SpecialMessageViewController *static_SpecialMessageViewController = nil;
static TabBarBaseViewController *static_TabBarBaseViewController = nil;

@implementation TransforController

+(TransforController *) sharedInstance {
    @synchronized (self)
    {
        if (shanrdObject == nil)
        {
            [[self alloc] init];
        }
    }
    return shanrdObject;
}

+(ThirdViewController *)sharedInstanceOfThirdViewController{
    if(static_ThirdViewController == nil){
        static_ThirdViewController = [[ThirdViewController alloc]init];
    }
    return static_ThirdViewController;
}

+(SecondViewController *)sharedInstanceOfSecondViewController{
    if(static_SecondViewController == nil){
        static_SecondViewController = [[SecondViewController alloc]init];
    }
    return static_SecondViewController;
}

+(PersonViewController *)sharedInstanceOfPersonViewController{
    if(static_PersonViewController == nil){
        static_PersonViewController = [[PersonViewController alloc]init];
    }
    return static_PersonViewController;
}

+(ToolsViewController *)sharedInstanceOfToolsViewController{
    if(static_ToolsViewController == nil){
        static_ToolsViewController = [[ToolsViewController alloc]init];
    }
    return static_ToolsViewController;
}

+(SpecialMessageViewController *)sharedInstanceOfSpecialMessageViewController {
    if (static_SpecialMessageViewController == nil) {
        static_SpecialMessageViewController = [[[SpecialMessageViewController alloc] initWithStyle:UITableViewStylePlain]autorelease];
    }
    return static_SpecialMessageViewController;
}

+(TabBarBaseViewController *)sharedInstanceOfTabBarBaseViewController {
    if (static_TabBarBaseViewController == nil) {
        static_TabBarBaseViewController = [[TabBarBaseViewController alloc] init];
    }
    return static_TabBarBaseViewController;
}

+(UIColor *)getColorWithRed:(int)red andGreen:(int)green andBlue:(int)blue andAlpha:(int)alpha{
    UIColor *color = [UIColor colorWithRed:red/255.0 green:green/255.0 blue:blue/255.0 alpha:alpha];
    return color;
}
+(UIColor *)getColorWithRGB:(int)rgb{
    UIColor *color = [Common colorWithRGB:rgb];
    return color;
}
+(UIFont *)getFontWithFont:(NSString *)font andSize:(CGFloat)size{
    UIFont *font_temp = [UIFont fontWithName:font size:size];
    return font_temp;
}
+(CGRect)getRectOfView:(UIView *)view withHasNavigationBar:(BOOL)hasNavigationBar andHasBottomBar:(BOOL)hasBottomBar{
    CGFloat x = view.frame.origin.x;
    CGFloat y = view.frame.origin.y;
    CGFloat width = view.frame.size.width;
    CGFloat height = view.frame.size.height;
    if(hasNavigationBar){
        y += (HEIGHT_NAVIGATION_BAR + HEIGHT_STATUS_BAR);
        height -= (HEIGHT_NAVIGATION_BAR + HEIGHT_STATUS_BAR);
    }
    if(hasBottomBar){
        height -= HEIGHT_BOTTOM_BAR;
    }
    CGRect rect = CGRectMake(x, y, width, height);
    return rect;
}

+(UIView *)shardeInstanceOfNavigationBarCoverViewWithY:(CGFloat)y{
    UIView *view_navagationBar = [[UIView alloc]initWithFrame:CGRectMake(0, y, 320, 45)];
    view_navagationBar.backgroundColor = [Common colorWithRGB:0x18aaf6];
    return view_navagationBar;
}
+(UIView *)shardeInstanceOfNavigationBarCoverViewWithY:(CGFloat)y andBackgroundColor:(UIColor *)color{
    UIView *view_navagationBar = [[UIView alloc]initWithFrame:CGRectMake(0, y, 320, 45)];
    view_navagationBar.backgroundColor = color;
    return view_navagationBar;
}
+(UIView *)shardeInstanceOfNavigationBarAndStatusBarCoverViewWithY:(CGFloat)y{
    UIView *view_navagationBar = [[UIView alloc]initWithFrame:CGRectMake(0, y, 320, 65)];
    view_navagationBar.backgroundColor = [Common colorWithRGB:0x18aaf6];
    return view_navagationBar;
}
+(UIView *)shardeInstanceOfNavigationBarCoverViewWithY:(CGFloat)y andTitle:(NSString *)title{
    UIView *view_navagationBar = [[UIView alloc]initWithFrame:CGRectMake(0, y, 320, 45)];
    view_navagationBar.backgroundColor = [Common colorWithRGB:0x18aaf6];
    UILabel *lb_title = [[UILabel alloc]initWithFrame:CGRectMake(0, 10, 320, 45)];
    lb_title.textColor = [UIColor whiteColor];
    lb_title.textAlignment = UITextAlignmentCenter;
    lb_title.font = [UIFont fontWithName:@"TrebuchetMS" size:16.0];
    lb_title.text = title;
    [view_navagationBar addSubview:lb_title];
    return view_navagationBar;
}

+(UIView *)shardeInstanceOfNavigationBarAndStatusBarCoverViewWithY:(CGFloat)y andTitle:(NSString *)title{
    UIView *view_navagationBar = [[UIView alloc]initWithFrame:CGRectMake(0, y, 320, 65)];
    view_navagationBar.backgroundColor = [Common colorWithRGB:0x18aaf6];
    UILabel *lb_title = [[UILabel alloc]initWithFrame:CGRectMake(0, 10, 320, 65)];
    lb_title.textColor = [UIColor whiteColor];
    lb_title.textAlignment = UITextAlignmentCenter;
    lb_title.font = [UIFont fontWithName:@"TrebuchetMS" size:16.0];
    lb_title.text = title;
    [view_navagationBar addSubview:lb_title];
    return view_navagationBar;
}

+ (int)getNSStringLength:(NSString*)strtemp
{
    int strlength = 0;
    char* p = (char*)[strtemp cStringUsingEncoding:NSUnicodeStringEncoding];
    for (int i=0 ; i<[strtemp lengthOfBytesUsingEncoding:NSUnicodeStringEncoding] ;i++) {
        if (*p) {
            p++;
            strlength++;
        }
        else {
            p++;
        }
        
    }
    return strlength;
}

+(NSString *)getLowOrHighCaseChangeWithString:(NSString *)string andIsChangeToHigh:(BOOL)boolean{
    NSString *string_temp = nil;
    if(boolean){
        string_temp = [string uppercaseString];
    }else{
        string_temp = [string lowercaseString];
    }
    return string_temp;
}

+ (void)testModel:(BOOL)boolean{
    if(boolean)
    [[Common shareInstance]showAlertViewWithTitle:nil text:@"切换到测试模式" dismissTime:1.0];
    isTestModel = boolean;
    
    Class cls = NSClassFromString(@"UMANUtil");
    SEL deviceIDSelector = @selector(openUDIDString);
    NSString *deviceID = nil;
    if(cls && [cls respondsToSelector:deviceIDSelector]){
        deviceID = [cls performSelector:deviceIDSelector];
    }
    NSData* jsonData = [NSJSONSerialization dataWithJSONObject:@{@"oid" : deviceID}
                                                       options:NSJSONWritingPrettyPrinted
                                                         error:nil];
    
    NSLog(@"%s || %@", __FUNCTION__, [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding]);
}
+ (BOOL)isTestModelOn{
    return isTestModel;
}

//邮箱
+ (BOOL) validateEmail:(NSString *)email
{
    NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
    return [emailTest evaluateWithObject:email];
}

//手机号码验证
+ (BOOL) validateMobile:(NSString *)mobile
{
    //手机号以13， 15，18开头，八个 \d 数字字符
    NSString *phoneRegex = @"^((13[0-9])|(15[^4,\\D])|(18[0,0-9]))\\d{8}$";
    NSPredicate *phoneTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",phoneRegex];
    return [phoneTest evaluateWithObject:mobile];
}

//用户名
+ (BOOL) validateUserName:(NSString *)name
{
    NSString *userNameRegex = @"^[A-Za-z0-9]{6,20}+$";
    NSPredicate *userNamePredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",userNameRegex];
    BOOL B = [userNamePredicate evaluateWithObject:name];
    return B;
}

//密码
+ (BOOL) validatePassword:(NSString *)passWord
{
    NSString *passWordRegex = @"^[a-zA-Z0-9]{6,20}+$";
    NSPredicate *passWordPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",passWordRegex];
    return [passWordPredicate evaluateWithObject:passWord];
}

//昵称
+ (BOOL) validateNickname:(NSString *)nickname
{
    NSString *nicknameRegex = @"^[\u4e00-\u9fa5]{4,8}$";
    NSPredicate *passWordPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",nicknameRegex];
    return [passWordPredicate evaluateWithObject:nickname];
}

//身份证号
+ (BOOL) validateIdentityCard: (NSString *)identityCard
{
    BOOL flag;
    if (identityCard.length <= 0) {
        flag = NO;
        return flag;
    }
    NSString *regex2 = @"^(\\d{14}|\\d{17})(\\d|[xX])$";
    NSPredicate *identityCardPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex2];
    return [identityCardPredicate evaluateWithObject:identityCard];
}

//umeng
+(void)umengEvent:(NSString *)eventId attributes:(NSDictionary *)attributes number:(NSNumber *)number ct:(NSString *)ct{
    NSString *numberKey = ct;
    NSMutableDictionary *mutableDictionary = [NSMutableDictionary dictionaryWithDictionary:attributes];
    [mutableDictionary setObject:[number stringValue] forKey:numberKey];
    [MobClick event:eventId attributes:mutableDictionary];
}


@end
